package com.sinosoft.perfect.system._authorization.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._authorization.dao.IRoleResourceAuthorizeDao;
import com.sinosoft.perfect.system._authorization.vo.RoleResourceRelationAuthorize;

public class RoleResourceAuthorizeImpl implements IRoleResourceAuthorizeService {

	private IRoleResourceAuthorizeDao  iroleResourceAuthorizeDao;	
	
	public void setIroleResourceAuthorizeDao(
			IRoleResourceAuthorizeDao iroleResourceAuthorizeDao) {
		this.iroleResourceAuthorizeDao = iroleResourceAuthorizeDao;
	}

	@Override
	public void deletePowerOfRoleResource(
			RoleResourceRelationAuthorize roleResourceRelationAuthorize)
			throws SQLException {
		iroleResourceAuthorizeDao.deletePowerOfRoleResource(roleResourceRelationAuthorize);
	}

	@Override
	public void insertByRoleResourceRelationAuthorize(
			RoleResourceRelationAuthorize roleResourceRelationAuthorize)
			throws SQLException {
		iroleResourceAuthorizeDao.insertByRoleResourceRelationAuthorize(roleResourceRelationAuthorize);
	}

	@Override
	public RoleResourceRelationAuthorize queryTRoleResourceRelationByResourceAndRole(
			String resourceId,String roleSystemId)
			throws SQLException {
		RoleResourceRelationAuthorize roleResourceRelationAuthorize = new RoleResourceRelationAuthorize();
		roleResourceRelationAuthorize.setResourceId(resourceId);
		roleResourceRelationAuthorize.setRoleSystemId(roleSystemId);
		
		RoleResourceRelationAuthorize resultClass  = 
			iroleResourceAuthorizeDao.queryTRoleResourceRelationByResourceAndRole(roleResourceRelationAuthorize);
		return resultClass;
	}

	@Override
	public void updatePowerOfRoleResource(
			RoleResourceRelationAuthorize roleResourceRelationAuthorize)
			throws SQLException {
		iroleResourceAuthorizeDao.updatePowerOfRoleResource(roleResourceRelationAuthorize);
	}

	@Override
	public List<String> queryResourceIdsOfRole(String roleSystemId)
			throws SQLException {
		List<String> resourceIds = iroleResourceAuthorizeDao.queryResourceIdsOfRole(roleSystemId);
		return resourceIds;
	}

	@Override
	public boolean operateResourceRoleAuthorize(String chooseIds,String roleSystemId) throws SQLException{
		//选择准备更新授权的ids
		String []chooseIdStrings = chooseIds.split(",");
		//库里面已经存在的授权信息
		List<String> resourceIds = this.queryResourceIdsOfRole(roleSystemId);
		
		List<RoleResourceRelationAuthorize> RoleResourceAuthorizeInsertsList = null;
		List<RoleResourceRelationAuthorize> RoleResourceAuthorizeDeleteList = null;
		
		for(int i=0;i<chooseIdStrings.length;i++){
			String chooseIdString = chooseIdStrings[i];
			boolean insertBool = true;
			if(resourceIds != null ){
				for(int j=0;j<resourceIds.size();j++){
					String resourceId = resourceIds.get(j);
					if(chooseIdString.equals(resourceId)){
						insertBool = false;
						resourceIds.remove(j);
						break;
					}
				}
			}
			if(insertBool){
				if(RoleResourceAuthorizeInsertsList == null)RoleResourceAuthorizeInsertsList 
					= new ArrayList<RoleResourceRelationAuthorize>();
				String id = Producer.generateIdOfTen();
				RoleResourceRelationAuthorize rrra = this.getRoleResourceRelationAuthorizeNew
					(chooseIdString, roleSystemId, id);
				RoleResourceAuthorizeInsertsList.add(rrra);
			}
		}
		
		if(resourceIds != null ){
			for(int j=0;j<resourceIds.size();j++){
				String resourceId = resourceIds.get(j);
				if(resourceId != null){
					if(RoleResourceAuthorizeDeleteList == null)RoleResourceAuthorizeDeleteList 
						= new ArrayList<RoleResourceRelationAuthorize>();
					RoleResourceRelationAuthorize rrra = this.getRoleResourceRelationAuthorizeNew
						(resourceId, roleSystemId, null);
					RoleResourceAuthorizeDeleteList.add(rrra);
				}
			}
		}
		
		boolean operateBooleanResult = true;
		try{
		iroleResourceAuthorizeDao.operateResourceRoleAuthorize
			(RoleResourceAuthorizeDeleteList, RoleResourceAuthorizeInsertsList);
		}catch (SQLException e) {
			e.printStackTrace();
			operateBooleanResult = false;
		}
		return operateBooleanResult;
	}
	
	
	
	private RoleResourceRelationAuthorize getRoleResourceRelationAuthorizeNew
		(String resourceId,String roleSystemId,String id){
		RoleResourceRelationAuthorize resuleClassAuthorize = new RoleResourceRelationAuthorize();
		resuleClassAuthorize.setResourceId(resourceId);
		resuleClassAuthorize.setRoleSystemId(roleSystemId);
		resuleClassAuthorize.setId(id);
		return resuleClassAuthorize;
	}
	

}
